import { ElNotification } from 'element-plus'

export const isDev = process.env.NODE_ENV === 'development'

// 格式化参数
export function setParams(params: any) {
  const searchParams = new URLSearchParams()

  Object.keys(params).forEach((key: any) => {
    searchParams.set(key, params[key])
  })

  return searchParams.toString()
}

// 输出通知
export const notice = (message: string, type: any = 'success', title = 'Message') => {
  ElNotification({
    title,
    message,
    type,
  })
}

// 浏览器环境
export const isBrowser = (): boolean => {
  return typeof window !== 'undefined'
}

// 处理当前状态
export const handleCurrentActive = (id: string, route: any) => {
  if (!isBrowser()) return
  console.log('route.path', route.path)

  if (location.href?.includes(route.path) || location.href?.includes(encodeURI(route.path))) {
    const ei = document.querySelector(id)
    ei && ei.classList.add('is-active')
  }
}

/**
 * @description 获取localStorage
 * @param {String} key Storage名称
 * @return string
 */
export function localGet(key: string) {
  const value = window.localStorage.getItem(key)
  if (!value) {
    return ''
  }

  return JSON.parse(window.localStorage.getItem(key) as string)
}

/**
 * @description 存储localStorage
 * @param {String} key Storage名称
 * @param {Any} value Storage值
 * @return void
 */
export function localSet(key: string, value: any) {
  if (!key) return ''
  window.localStorage.setItem(key, JSON.stringify(value))
}

/**
 * @description 清除localStorage
 * @param {String} key Storage名称
 * @return void
 */
export function localRemove(key: string) {
  if (!key) return ''
  window.localStorage.removeItem(key)
}

/**
 * @description 清除所有localStorage
 * @return void
 */
export function localClear() {
  window.localStorage.clear()
}

// 获取子目录
export const fetchChildColumns = (data: any) => {
  const apiUrl = import.meta.env.VITE_API_URL

  const requests = data.map((item: any) => {
    const url = `${apiUrl}column/getChildColumns/${item.id}`

    return useHttpGet('getChildColumns', url, { lazy: true })
  })

  return Promise.all(requests)
}

// 获取详细页面链接
export const getDetailLink = (link: any, sub: any) => {
  const path = `/bes/${link.name}/${sub?.name}`
  const query = { columnId: link?.id, columnName: link?.name, childColumnId: sub?.id, childColumnName: sub?.name }

  return { path, query }
}
